<?php

class model_vsAccount extends L {

    /**
     * 描述 ：分页的查询语句拼接
     * 作者 ：Zoro.Zhu
    **/
    public static function searchSql($params = array()){
        $sql = "SELECT
             `account`, `appKey`, `appSecret`, `accessToken`, `refreshToken`, `invalidDate`, `state`, `type`, `addDate`, `data`
        FROM
            `vs_account`
        WHERE 1";
        $user = core_base::getUser('name');
        $sql .= " AND `addName` = '{$user}'";
        $sql .= !empty($params['search']['account']) ? " AND `account` = '{$params['search']['account']}' " : "";
        $sql .= !empty($params['search']['accounts']) ? " AND `account` IN ('" . join("','", $params['search']['accounts']) . "') " : "";
        $sql .= !empty($params['search']['appKey']) ? " AND `appKey` = '{$params['search']['appKey']}' " : "";
        $sql .= isset($params['search']['state']) && $params['search']['state'] !== '' ? " AND `state` = '{$params['search']['state']}' " : "";
        $sql .= !empty($params['search']['type']) ? " AND `type` = '{$params['search']['type']}' " : "";
        return $sql;
    }

    /**
     * 描述 ：执行插入数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function insertSql($data = array()){
        empty($data['state']) && $data['state'] = 0;
        $user = core_base::getUser('name');
        $sql = "INSERT INTO `vs_account`(
             `account`,`appKey`, `appSecret`, `accessToken`, `refreshToken`, `invalidDate`, `state`, `type`, `addDate`, `data`,`addName`
        ) VALUES (
             '{$data['account']}','{$data['appKey']}', '{$data['appSecret']}', '{$data['accessToken']}', '{$data['refreshToken']}', '{$data['invalidDate']}', '{$data['state']}', '{$data['type']}', '{$data['addDate']}', '{$data['data']}', '{$user}'
        ) ON DUPLICATE KEY UPDATE
        `appKey`=VALUES(`appKey`),
        `appSecret`=VALUES(`appSecret`),
        `accessToken`=VALUES(`accessToken`),
        `refreshToken`=VALUES(`refreshToken`),
        `invalidDate`=VALUES(`invalidDate`),
        `state`=VALUES(`state`),
        `type`=VALUES(`type`),
        `addDate`=VALUES(`addDate`),
        `addName`=VALUES(`addName`),
        `data`=VALUES(`data`)";
        return L::sql($sql);
    }

    /**
     * 描述：根据主键获取数据
     * 作者：Zoro.Zhu
    **/
    public static function getKeyInfo($key){
        $sql = "SELECT
             `account`, `appKey`, `appSecret`, `accessToken`, `refreshToken`, `invalidDate`, `state`, `type`, `addDate`, `data`
        FROM
            `vs_account`
        WHERE 
            `account` = '{$key}'";
        return L::sql($sql);
    }

    /**
     * 描述：获取账号授权信息
     * 参数：{
     *      account ： 销售账号
     *      type : 平台类型
     * }
     * 返回值：{
     *      account ：销售账号
     *      appkey ：应用ID
     *      appSecret ：应用密钥
     *      accessToken ：访问令牌
     *      refreshToken ：刷新令牌
     *      invalidDate ：访问令牌过期时间
     *      state ：状态
     *      type ：类型
     *      addDate ：添加时间
     * }
     * 作者：Zoro.Zhu
     **/
    public static function getAccountToken($params){
        $sql = "SELECT
          `account`, `appKey`, `appSecret`, `accessToken`, `refreshToken`,
          `invalidDate`, `state`, `type`, `addDate`
        FROM
          `vs_account`
        WHERE `state` = '0' ";
        !empty($params['account']) && $sql .= " AND `account` = '{$params['account']}'";
        !empty($params['type']) && $sql .= " AND `type` = '{$params['type']}'";
        $sql .= " LIMIT 1";
        $info = L::sql($sql);
        return !empty($info[0]) ? $info[0] : array();
    }

    /**
     * 描述 ：根据平台类型获取有效的账号
     * 参数 ：$type ：账号类型
     * 作者 ：Zoro.Zhu
    **/
    public static function getAccountList($type = null){
        $sql = "SELECT
            `account` 
        FROM
            `vs_account` 
        WHERE
            `state` = '0'";
        $type && $sql .= " AND `type` = '{$type}' ";
        $info = L::sql($sql);
        $data = [];
        foreach($info as $row){
            $data[] = $row['account'];
        }
        return $data;

    }

}